1 using System;
2 using
System.Collections;
3 using
UnityEngine;
4 using
UnityStandardAssets.Utility;
5
6 namespace
UnityStandardAssets.Effects
7 {
8     
public class Explosive : MonoBehaviour
9     {
10         
public Transform explosionPrefab;
11         
public float detonationImpactVelocity = 10;
12         
public float sizeMultiplier = 1;
13         
public bool reset = true;
14         
public float resetTimeDelay = 10;
15
16         
private bool m_Exploded;
17         
private ObjectResetter m_ObjectResetter;
18
19
20         
// implementing one method from monobehviour to ensure that the enable/disable tickbox appears in the inspector
21         
private void Start()
22         {
23             m_ObjectResetter = GetComponent<ObjectResetter>();
24         }
25
26
27         
private IEnumerator OnCollisionEnter(Collision col)
28         {
29             
if (enabled)
30             {
31                 
if (col.contacts.Length > 0)
32                 {
33                     
// compare relative velocity to collision normal - so we don't explode from a fast but gentle glancing collision
34                     
float velocityAlongCollisionNormal =
35                         Vector3.Project(col.relativeVelocity, col.contacts[
0].normal).magnitude;
36
37                     
if (velocityAlongCollisionNormal > detonationImpactVelocity || m_Exploded)
38                     {
39                         
if (!m_Exploded)
40                         {
41                             Instantiate(explosionPrefab, col.contacts[
0].point,
42                                         Quaternion.LookRotation(col.contacts[
0].normal));
43                             m_Exploded =
true;
44
45                             SendMessage(
"Immobilize");
46
47                             
if (reset)
48                             {
49                                 m_ObjectResetter.DelayedReset(resetTimeDelay);
50                             }
51                         }
52                     }
53                 }
54             }
55
56             
yield return null;
57         }
58
59
60         
public void Reset()
61         {
62             m_Exploded =
false;
63         }
64     }
65 }


Gõ tìm kiếm nhanh...